package com.cgbl.service.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import com.cgbl.service.R
import com.cgbl.service.databinding.AppFragmentMessageBinding
import com.cgbl.service.databinding.AppItemMessageBinding
import com.cgbl.service.im.viewholder.CustomP2PVIewHolder
import com.cgbl.service.im.viewholder.CustomTeamViewHolder
import com.cgbl.service.pop.MessageFunctionPop
import com.cgbl.service.vm.MessageFragmentViewModel
import com.lxj.xpopup.XPopup
import com.netease.yunxin.kit.chatkit.ui.ChatKitClient
import com.netease.yunxin.kit.chatkit.ui.custom.ChatOrderViewHolder
import com.netease.yunxin.kit.chatkit.ui.custom.ChatStickerViewHolder
import com.netease.yunxin.kit.chatkit.ui.custom.OrderAttachment
import com.netease.yunxin.kit.chatkit.ui.custom.StickerAttachment
import com.netease.yunxin.kit.common.ui.viewholder.BaseViewHolder
import com.netease.yunxin.kit.conversationkit.ui.ConversationKitClient
import com.netease.yunxin.kit.conversationkit.ui.ConversationUIConfig
import com.netease.yunxin.kit.conversationkit.ui.common.ConversationConstant
import com.netease.yunxin.kit.conversationkit.ui.model.ConversationBean
import com.netease.yunxin.kit.conversationkit.ui.page.ConversationFragment
import com.netease.yunxin.kit.conversationkit.ui.page.DefaultViewHolderFactory
import com.robot.base.base.activity.GLBaseFragment
import me.goldze.mvvmhabit.BR


class MessageFragment : GLBaseFragment<AppFragmentMessageBinding, MessageFragmentViewModel>() {

    override fun initContentView(
        inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_message

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        initView()
    }

    private fun initView() {
//        messageAdapter = MessageAdapter()
//        binding.recyclerView.apply {
//            layoutManager = LinearLayoutManager(context)
//            adapter = messageAdapter
//        }
//        messageAdapter.setNewInstance(mutableListOf("", ""))
        val fragmentManager: FragmentManager? = fragmentManager
        //创建ConversationFragment
        //创建ConversationFragment
        val fragment = ConversationFragment()
        //将ConversationFragment加载到Activity中
        //将ConversationFragment加载到Activity中
        fragmentManager?.beginTransaction()?.add(R.id.conversation_container, fragment)?.commit()

        val config = ConversationUIConfig()
        //2、设置TitleBar不可见
        config.showTitleBar = false;

        config.conversationFactory = object : DefaultViewHolderFactory() {
            override fun createViewHolder(
                parent: ViewGroup, viewType: Int
            ): BaseViewHolder<ConversationBean>? {
                if (viewType == ConversationConstant.ViewType.CHAT_VIEW) {
                    return CustomP2PVIewHolder(
                        AppItemMessageBinding.inflate(
                            LayoutInflater.from(parent.context), parent, false
                        )
                    )
                } else if (viewType == ConversationConstant.ViewType.TEAM_VIEW) {
                    return CustomTeamViewHolder(
                        AppItemMessageBinding.inflate(
                            LayoutInflater.from(parent.context), parent, false
                        )
                    )
                }
                return null
            }
        }
        //3、将自定义事件设置到ConversationKitClient即可
        ConversationKitClient.setConversationUIConfig(config);
    }

    private fun initTiterBar() {
        val titleView = view?.findViewById<TextView>(R.id.tv_title)
        val backView = view?.findViewById<ImageView>(R.id.iv_back)
        titleView?.text = "消息列表"
        backView?.visibility = View.GONE
    }

    override fun initViewObservable() {
        viewModel.uc.showAddFunctionEvent.observe(this) {
            val messageFunctionPop = context?.let { it1 -> MessageFunctionPop(it1) }
            XPopup.Builder(context).atView(binding.ivAdd).asCustom(messageFunctionPop).show()
        }
    }
}